import hashlib

from PIL import Image
from scipy import signal

from thread_project.demo02.modules.base import BaseModule
from thread_project.demo02.modules.excutors import thread_pool_executor as tp
from thread_project.demo02.modules.excutors import process_pool_executor as pp


class Hasher(BaseModule):
    def _process(self, item):
        cov = [[[0.1], [0.05], [0.1]]]
        img = signal.convolve(item, cov)
        img = Image.fromarray(img.astype("uint8")).convert("RGB")
        md5 = hashlib.md5(str(img).encode("utf-8")).hexdigest()
        return md5

    def _process_single_thread(self, list_):
        md5_list = []
        for img in list_:
            md5 = self._process(img)
            md5_list.append(md5)

        return md5_list

    def _process_multi_thread(self, list_):
        md5_list = []
        task_list = []

        for img in list_:
            task = tp.submit(self._process, (img,))
            task_list.append(task)

        for task in task_list:
            md5 = task.result()
            md5_list.append(md5)

        return md5_list

    def _process_multi_process(self, list_):
        md5_list = []
        task_list = []

        for img in list_:
            task = pp.submit(self._process, (img))
            task_list.append(task)

        for task in task_list:
            md5 = task.result()
            md5_list.append(md5)

        return md5_list
